package com.example.micro.es.api.service.impl;

import com.example.micro.es.api.entity.Device;
import com.example.micro.es.api.entity.DeviceExample;
import com.example.micro.es.api.mapper.DeviceExpandMapper;
import com.example.micro.es.api.mapper.DeviceMapper;
import com.example.micro.es.api.mapper.TestUpdateExpandMapper;
import com.example.micro.es.api.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * @author Code Farmer
 * @create 2020/11/9 5:01 下午
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    private static int index = 0;

    private static String business[] = {"橙心优选", "滴滴外卖", "顺风车"};
    private static String city[] = {"北京", "成都", "杭州"};
    private static String idx2[] = {"1", "2", "3", "4", "5"};

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceExpandMapper deviceExpandMapper;

    @Autowired
    private TestUpdateExpandMapper testUpdateExpandMapper;

    @Override
    @Transactional
    public void updateReadLock() {
        try {
            Random random = new Random();
            Device device = new Device();
            device.setCount(index++);
            DeviceExample deviceExample = new DeviceExample();
            deviceExample.createCriteria()
                    .andBusinessEqualTo("橙心优选")
                    .andCityEqualTo("成都");
            deviceExpandMapper.updateCount("橙心优选", "成都");
        } catch (Exception ex) {
            throw ex;
        }

    }

    @Override
//    @Transactional
    public void updateReadLock1() {
        try {
//            Random random = new Random();
//            Device device2 = new Device();
//            device2.setCount(index++);
//            DeviceExample deviceExample2 = new DeviceExample();
//            deviceExample2.createCriteria()
//                    .andBusinessEqualTo("滴滴外卖")
//                    .andCityEqualTo("杭州");
            int c = rand();
            int b = rand();
            deviceExpandMapper.updateCount(business[b], city[c]);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    @Transactional
    public void updateReadLock3() {
        try {
            int b = idxrand();
            testUpdateExpandMapper.updateStatus(idx2[b]);
        } catch (Exception ex) {
            throw ex;
        }
    }

    private int idxrand() {
        int max = 5, min = 0;
        int ran2 = (int) (Math.random() * (max - min) + min);
        return ran2;
    }

    private int rand() {
        int max = 3, min = 0;
        int ran2 = (int) (Math.random() * (max - min) + min);
        return ran2;
    }

    public static void main(String[] args) {
//        System.out.println((int) (Math.random() * 3));
        while (true) {
            int max = 5, min = 0;
            int ran2 = (int) (Math.random() * (max - min) + min);
            System.out.println(ran2);
        }


    }

}
